package vn.com.vega.chacha.v2;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.music.clientserver.JsonAlbum;
import vn.com.vega.music.clientserver.JsonArtist;
import vn.com.vega.music.clientserver.JsonPlaylist;
import vn.com.vega.music.clientserver.JsonSong;
import vn.com.vega.music.clientserver.JsonVideo;
import vn.com.vega.music.utils.Const;
import vn.com.vega.music.view.adapter.AlbumListAdapter;
import vn.com.vega.music.view.adapter.ArtistListAdapter;
import vn.com.vega.music.view.adapter.PlaylistListAdapter;
import vn.com.vega.music.view.adapter.SongListAdapter;
import vn.com.vega.music.view.adapter.VideoListAdapter;
import vn.com.vega.music.view.adapter.base.AutoLoadingAdapter;
import vn.com.vega.music.view.adapter.base.AutoLoadingListener;
import vn.com.vega.music.view.custom.BottomPlayerView;
import vn.com.vega.music.view.custom.BottomPlayerView.ShowHideBottomPlayerListener;
import vn.com.vega.music.view.custom.LoadingView;
import vn.com.vega.music.view.custom.LoadingView.RetryListener;
import vn.com.vega.music.view.custom.TopTaskbarView;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class LikedScreen extends ActivityBase implements AutoLoadingListener, OnClickListener {
	public static final int INDEX_LIST_SONG_LIKED = 0;
	public static final int INDEX_LIST_VIDEO_LIKED = 1;
	public static final int INDEX_LIST_ALBUM_LIKED = 2;
	public static final int INDEX_LIST_PLAYLST_LIKED = 3;
	public static final int INDEX_LIST_ARTIST_LIKED = 4;

	private int currentFillter = INDEX_LIST_SONG_LIKED;

	private TopTaskbarView mTopTaskbarView;
	private View mFillterView;
	private TextView mFillterLabel;
	private LoadingView mLoadingView;
	private ListView mListView;
	private SongListAdapter mSongAdapter;
	private VideoListAdapter mVideoAdapter;
	private AlbumListAdapter mAlbumAdapter;
	private PlaylistListAdapter mPlaylistAdapter;
	private ArtistListAdapter mArtistAdapter;
	private List<Object> mListSongLiked;
	private List<Object> mListVideoLiked;
	private List<Object> mListAlbumliked;
	private List<Object> mListPlaylistLiked;
	private List<Object> mListArtistLiked;

	private LoadLikedList mLoadLikedList;

	private ArrayList<String> fillterListString = new ArrayList<String>();
	private static final String DIALOG_FILLTER_LIKED = "dialog fillter liked";
	
	private BottomPlayerView mBottomPlayer;
	private View mPlayerViewReplace;
	
	private View mPlayAllSong;
	private View mSelectSongPlay;
	private View mHeaderListSong;

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.like_screen);
		mTopTaskbarView = (TopTaskbarView) findViewById(R.id.topTaskbarView_liked_screen);
		mTopTaskbarView.showBackButton();
		mTopTaskbarView.showMainMenu(false);
		mTopTaskbarView.changeToTitleOnly(getString(R.string.list_liked_song));

		mFillterView = findViewById(R.id.layout_liked_fillter);
		mFillterLabel = (TextView) findViewById(R.id.tv_liked_fillter);
		mLoadingView = (LoadingView) findViewById(R.id.loadingView_loading_list_liked);
		mListView = (ListView) findViewById(R.id.lv_list_liked);

		fillterListString.add(getString(R.string.fillter_music));
		fillterListString.add(getString(R.string.fillter_video));
		fillterListString.add(getString(R.string.fillter_album));
		fillterListString.add(getString(R.string.fillter_playlist));
		fillterListString.add(getString(R.string.fillter_artist));

		mFillterLabel.setText(getString(R.string.fillter_music));
		mFillterView.setOnClickListener(selectFillterClickListener);
		
		mPlayerViewReplace = findViewById(R.id.view_replace_bottom_player);
		mBottomPlayer = (BottomPlayerView) findViewById(R.id.bottomPlayerView_like_screen);
		mBottomPlayer.setShowHideListener(showHideBottomPlayerListener);

		mLoadingView.setRetryListener(retryListener);
		
		mSelectSongPlay = findViewById(R.id.tv_select_song_play);
		mPlayAllSong = findViewById(R.id.tv_play_all_song);
		mSelectSongPlay.setOnClickListener(this);
		mPlayAllSong.setOnClickListener(this);
		mHeaderListSong = findViewById(R.id.layout_header_list_song);
		mHeaderListSong.setVisibility(View.GONE);
		
		loadListLike();
	}
	
	ShowHideBottomPlayerListener showHideBottomPlayerListener = new ShowHideBottomPlayerListener() {

		@Override
		public void onShowBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.VISIBLE);
		}

		@Override
		public void onHideBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.GONE);
		}
	};
	
	@Override
	protected void onResume() {
		super.onResume();
		mBottomPlayer.startUpdate();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		mBottomPlayer.stopUpdate();
	}

	private RetryListener retryListener = new RetryListener() {

		@Override
		public void onRetry() {
			// TODO Auto-generated method stub
			loadListLike();
		}
	};

	private void loadListLike() {
		if (mLoadLikedList != null) {
			mLoadLikedList.cancel(true);
		}
		mLoadLikedList = new LoadLikedList(this, currentFillter);
		mLoadLikedList.execute();
	}

	View.OnClickListener selectFillterClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showFilltter(fillterListString, selectListLikeListener,
					DIALOG_FILLTER_LIKED);
		}
	};

	AdapterView.OnItemClickListener selectListLikeListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			// TODO Auto-generated method stub
			hideDialogFillter(DIALOG_FILLTER_LIKED);
			chanegListLiked(position);
		}
	};

	private void showListLiked() {
		switch (currentFillter) {
		case INDEX_LIST_SONG_LIKED:
			trackPageView("D/s bài hát yêu thích");
			mHeaderListSong.setVisibility(View.VISIBLE);
			if (mListSongLiked == null) {
				mLoadingView.loadingFinish(false, null);
			} else if (mListSongLiked.size() == 0) {
				mLoadingView.loadingFinish(false,
						getString(R.string.you_do_not_like_any_song));
			} else {
				// Show list like album
				mLoadingView.loadingFinish(true, null);
				mSongAdapter = new SongListAdapter(this, mListSongLiked, false,
						LikedScreen.class.getName());
				mListView
						.setAdapter(new AutoLoadingAdapter(mSongAdapter, this, Const.NO_LIMIT));
			}
			break;
		case INDEX_LIST_VIDEO_LIKED:
			trackPageView("D/s video yêu thích");
			if (mListVideoLiked == null) {
				mLoadingView.loadingFinish(false, null);
			} else if (mListVideoLiked.size() == 0) {
				mLoadingView.loadingFinish(false,
						getString(R.string.you_do_not_like_any_video));
			} else {
				// Show list like album
				mLoadingView.loadingFinish(true, null);
				mVideoAdapter = new VideoListAdapter(this, mListVideoLiked,
						false);
				mListView
						.setAdapter(new AutoLoadingAdapter(mVideoAdapter, this, Const.NO_LIMIT));
			}
			break;
		case INDEX_LIST_ALBUM_LIKED:
			trackPageView("D/s album yêu thích");
			if (mListAlbumliked == null) {
				mLoadingView.loadingFinish(false, null);
			} else if (mListAlbumliked.size() == 0) {
				mLoadingView.loadingFinish(false,
						getString(R.string.you_do_not_like_any_album));
			} else {
				// Show list like album
				mLoadingView.loadingFinish(true, null);
				mAlbumAdapter = new AlbumListAdapter(this, mListAlbumliked);
				mListView
						.setAdapter(new AutoLoadingAdapter(mAlbumAdapter, this, Const.NO_LIMIT));
			}
			break;
		case INDEX_LIST_PLAYLST_LIKED:
			trackPageView("D/s playlist yêu thích");
			if (mListPlaylistLiked == null) {
				mLoadingView.loadingFinish(false, null);
			} else if (mListPlaylistLiked.size() == 0) {
				mLoadingView.loadingFinish(false,
						getString(R.string.you_do_not_like_any_playlist));
			} else {
				// Show list like album
				mLoadingView.loadingFinish(true, null);
				mPlaylistAdapter = new PlaylistListAdapter(this,
						mListPlaylistLiked);
				mListView.setAdapter(new AutoLoadingAdapter(mPlaylistAdapter,
						this, Const.NO_LIMIT));
			}
			break;
		case INDEX_LIST_ARTIST_LIKED:
			trackPageView("D/s Bài ca sỹ thích");
			if (mListArtistLiked == null) {
				mLoadingView.loadingFinish(false, null);
			} else if (mListArtistLiked.size() == 0) {
				mLoadingView.loadingFinish(false,
						getString(R.string.you_do_not_like_any_artist));
			} else {
				// Show list like album
				mLoadingView.loadingFinish(true, null);
				mArtistAdapter = new ArtistListAdapter(this, mListArtistLiked);
				mListView.setAdapter(new AutoLoadingAdapter(mArtistAdapter,
						this));
			}
			break;

		default:
			break;
		}
	}

	private String getTopTaskTitle() {
		switch (currentFillter) {
		case INDEX_LIST_SONG_LIKED:
			return getString(R.string.list_liked_song);
		case INDEX_LIST_VIDEO_LIKED:
			return getString(R.string.list_liked_video);
		case INDEX_LIST_ALBUM_LIKED:
			return getString(R.string.list_liked_album);
		case INDEX_LIST_PLAYLST_LIKED:
			return getString(R.string.list_liked_playlist);
		case INDEX_LIST_ARTIST_LIKED:
			return getString(R.string.list_liked_artist);
		default:
			return getString(R.string.list_liked_song);
		}
	}

	private void chanegListLiked(int likedType) {
		if (currentFillter == likedType) {
			// Do nothing
			return;
		}
		mHeaderListSong.setVisibility(View.GONE);
		currentFillter = likedType;
		mFillterLabel.setText(fillterListString.get(likedType));
		mTopTaskbarView.changeToTitleOnly(getTopTaskTitle());

		switch (currentFillter) {
		case INDEX_LIST_SONG_LIKED:
			if (mListSongLiked != null && mListSongLiked.size() > 0) {
				showListLiked();
			} else {
				loadListLike();
			}
			break;
		case INDEX_LIST_ALBUM_LIKED:
			if (mListAlbumliked != null && mListAlbumliked.size() > 0) {
				showListLiked();
			} else {
				loadListLike();
			}
			break;
		case INDEX_LIST_ARTIST_LIKED:
			if (mListArtistLiked != null && mListArtistLiked.size() > 0) {
				showListLiked();
			} else {
				loadListLike();
			}
			break;
		case INDEX_LIST_PLAYLST_LIKED:
			if (mListPlaylistLiked != null && mListPlaylistLiked.size() > 0) {
				showListLiked();
			} else {
				loadListLike();
			}
			break;
		case INDEX_LIST_VIDEO_LIKED:
			if (mListVideoLiked != null && mListVideoLiked.size() > 0) {
				showListLiked();
			} else {
				loadListLike();
			}
			break;

		default:
			break;
		}
	}

	private List<Object> loadListSongFavorite(int offset) {
		List<Object> listSong = null;
		JsonSong jsonSong = JsonSong.loadListFavoriteSong(offset);
		if (jsonSong.isSuccess()) {
			listSong = new ArrayList<Object>();
			if (jsonSong.songs != null && jsonSong.songs.size() > 0) {
				listSong.addAll(jsonSong.songs);
			}
		} else {
			listSong = null;
		}
		return listSong;
	}

	private List<Object> loadListVideoFavorite(int offset) {
		List<Object> listVideo = null;
		JsonVideo jsonVideo = JsonVideo.loadFavorite(offset);
		if (jsonVideo.isSuccess()) {
			listVideo = new ArrayList<Object>();
			if (jsonVideo.videos != null && jsonVideo.videos.size() > 0) {
				listVideo.addAll(jsonVideo.videos);
			}
		} else {
			listVideo = null;
		}
		return listVideo;
	}

	private List<Object> loadListAlbumFavorite(int offset) {
		List<Object> result = null;
		JsonAlbum jsonAlbum = JsonAlbum.loadFavoriteAlbum(offset);
		if (jsonAlbum.isSuccess()) {
			result = new ArrayList<Object>();
			if (jsonAlbum.albums != null && jsonAlbum.albums.size() > 0) {
				result.addAll(jsonAlbum.albums);
			}
		} else {
			result = null;
		}
		return result;
	}

	private List<Object> loadListPlaylistFavorite(int offset) {
		List<Object> result = null;
		JsonPlaylist jsonPlaylist = JsonPlaylist
				.loadListFavoritePlaylist(offset);
		if (jsonPlaylist.isSuccess()) {
			result = new ArrayList<Object>();
			if (jsonPlaylist.playlists != null
					&& jsonPlaylist.playlists.size() > 0) {
				result.addAll(jsonPlaylist.playlists);
			} else {
				result = null;
			}
		}
		return result;
	}

	private List<Object> loadListArtistFavorite(int offset) {
		List<Object> result = null;
		JsonArtist jsonArtist = JsonArtist.loadFavoriteArtistList(offset);
		if (jsonArtist.isSuccess()) {
			result = new ArrayList<Object>();
			if (jsonArtist.artists != null && jsonArtist.artists.size() > 0) {
				result.addAll(jsonArtist.artists);
			} else {
				result = null;
			}
		}
		return result;
	}

	static class LoadLikedList extends AsyncTask<Void, Void, Void> {
		WeakReference<LikedScreen> likedScreenRfc;
		int typeLike;

		public LoadLikedList(LikedScreen likeScreen, int likeType) {
			likedScreenRfc = new WeakReference<LikedScreen>(likeScreen);
			typeLike = likeType;
		}

		private boolean checkisLive() {
			LikedScreen likedScreen = likedScreenRfc.get();
			if (likedScreen == null || likedScreen.isDestroy) {
				return false;
			}
			return true;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			if (!checkisLive()) {
				return null;
			}
			LikedScreen likedScreen = likedScreenRfc.get();
			switch (typeLike) {
			case INDEX_LIST_SONG_LIKED:
				likedScreen.mListSongLiked = likedScreen
						.loadListSongFavorite(0);
				break;
			case INDEX_LIST_VIDEO_LIKED:
				likedScreen.mListVideoLiked = likedScreen
						.loadListVideoFavorite(0);
				break;
			case INDEX_LIST_ALBUM_LIKED:
				likedScreen.mListAlbumliked = likedScreen
						.loadListAlbumFavorite(0);
				break;
			case INDEX_LIST_PLAYLST_LIKED:
				likedScreen.mListPlaylistLiked = likedScreen
						.loadListPlaylistFavorite(0);
				break;
			case INDEX_LIST_ARTIST_LIKED:
				likedScreen.mListArtistLiked = likedScreen
						.loadListArtistFavorite(0);
				break;

			default:
				break;
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if (!checkisLive() || isCancelled()) {
				return;
			}
			LikedScreen likedScreen = likedScreenRfc.get();
			likedScreen.showListLiked();
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			if (!checkisLive()) {
				return;
			}
			LikedScreen likedScreen = likedScreenRfc.get();
			likedScreen.mLoadingView.startLoading();
		}

	}

	@Override
	public Activity getContext() {
		// TODO Auto-generated method stub
		return LikedScreen.this;
	}

	@Override
	public List<Object> getMoreData() {
		// TODO Auto-generated method stub
		List<Object> result = new ArrayList<Object>();
		switch (currentFillter) {
		case INDEX_LIST_SONG_LIKED:
			result = loadListSongFavorite(mListSongLiked.size());
			break;
		case INDEX_LIST_VIDEO_LIKED:
			result = loadListVideoFavorite(mListVideoLiked.size());
			break;
		case INDEX_LIST_ALBUM_LIKED:
			result = loadListAlbumFavorite(mListAlbumliked.size());
			break;
		case INDEX_LIST_PLAYLST_LIKED:
			result = loadListPlaylistFavorite(mListPlaylistLiked.size());
			break;
		case INDEX_LIST_ARTIST_LIKED:
			result = loadListArtistFavorite(mListArtistLiked.size());
			break;

		default:
			break;
		}
		return result;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (currentFillter != INDEX_LIST_SONG_LIKED) {
			return;
		}
		switch (v.getId()) {
		case R.id.tv_select_song_play:
			if (mSongAdapter != null && mSongAdapter.getCount() > 0) {
				mSongAdapter.selectSongPlay("Các bài hát yêu thích");
			}
			break;
		case R.id.tv_play_all_song:
			if (mSongAdapter != null && mSongAdapter.getCount() > 0) {
				mSongAdapter.playAllSong("Các bài hát yêu thích");
			}
			break;

		default:
			break;
		}
	}

}
